package com.cxdr.awsips.packing.bean;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.log4j.Logger;

import com.cxdr.awsips.packing.logger.LogFactory;
import com.cxdr.awsips.packing.processor.StorePackLogProccessor;

/**
 * 打包日志
 * @author pugang
 *
 */
public class PackLog implements Cloneable{
	private static Logger logger = LogFactory.makeNewLogger(PackLog.class);
	private String ID;
	private String partition_month; //分区月
	private String source_file; //源文件
	private Date create_time;
	private String target_file; //打包文件名
	private String file_type; //文件类型
	private String create_day;
	private String create_hour; 
	private String station_ID; //区站号
	public PackLog(){
	}
	public PackLog(String fileName){
		source_file = fileName;
		parseAttr();
		create_day = String.valueOf(create_time.getDate());
		create_hour = String.valueOf(create_time.getHours());
	}
	private void parseAttr(){
		_parseFileNameAttr(source_file);
	}
	private void _parseFileNameAttr(String fileName){
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM");
		String[] infos = fileName.split("_");
		for(int i =0;i<infos.length;i++){
			String info = infos[i];
			if(info.length()==14){
				//记录时间
				try {
					create_time =  format.parse(info);
					this.partition_month=format1.format(create_time);
				} catch (ParseException e) {
					e.printStackTrace();
					create_time = new Date();
				}
				continue;
			}
			//站号
			if("I".equalsIgnoreCase(info)){
				info=infos[++i];
				if(info.contains("-REG")){
					info = info.replaceAll("-REG", "");
				}
				station_ID = info;
				continue;
			}
			//文件类型
		}
		if(fileName.contains("O_AWS_FTM")){
			file_type=FileType.E.name();//"地面气象要素数据文件";
		}
		if(fileName.contains("O_AWS-PRF_FTM")){
			file_type=FileType.N.name();//"地面自动站雨量观测资料";
		}
		if(fileName.contains("O_AWS_DAY")){
			file_type=FileType.D.name();//"自动站日数据文件";
		}
		if(fileName.contains("R_AWS_FTM")){
			file_type=FileType.Z.name();//"自动站状态信息文件";
		}
		if(fileName.contains("O_ARS_FTM")){
			file_type=FileType.R.name();//"自动站气象辐射资料";
		}
	}
	public String getID() {
		return ID;
	}
	public void setID(String iD) {
		ID = iD;
	}
	public String getPartition_month() {
		return partition_month;
	}
	public void setPartition_month(String partition_month) {
		this.partition_month = partition_month;
	}
	public String getSource_file() {
		return source_file;
	}
	public void setSource_file(String source_file) {
		this.source_file = source_file;
	}
	public Date getCreate_time() {
		return create_time;
	}
	public void setCreate_time(Date create_time) {
		this.create_time = create_time;
	}
	public String getTarget_file() {
		return target_file;
	}
	public void setTarget_file(String target_file) {
		this.target_file = target_file;
	}
	public String getFile_type() {
		return file_type;
	}
	public void setFile_type(String file_type) {
		this.file_type = file_type;
	}
	public String getCreate_day() {
		return create_day;
	}
	public void setCreate_day(String create_day) {
		this.create_day = create_day;
	}
	public String getCreate_hour() {
		return create_hour;
	}
	public void setCreate_hour(String create_hour) {
		this.create_hour = create_hour;
	}
	public String getStation_ID() {
		return station_ID;
	}
	public void setStation_ID(String station_ID) {
		this.station_ID = station_ID;
	}
	@Override
	public PackLog clone(){
		try {
			Object obj = super.clone();
			return (PackLog)obj;
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
			logger.error("",e);
			PackLog log = new PackLog();
			log.setSource_file(this.source_file);
			log.setTarget_file(this.getTarget_file());
			log.setCreate_time(this.create_time);
			return log;
		}
	}
	@Override
	public String toString(){
		return DateFormatUtils.format(create_time, "HH:mm")+" "+this.source_file+"打包到"+this.target_file;
	}
	public static void main(String[] args) {
		PackLog log =new PackLog();
		log.setSource_file("source");
		log.setTarget_file("target");
		log.setCreate_time(new Date());
		PackLog log2 = log.clone();
		System.out.println(log2.equals(log));
		System.out.println(log2.toString());
	}
}
